﻿/*************************************************************************
 *
 * Copyright (c) 2009-2014 Xuld. All rights reserved.
 * 
 * Project Url: http://work.xuld.net/circus
 * 
 * This source code is part of the Project Circus.
 * 
 * This code is licensed under The Circus License.
 * See the file License.html for the license details.
 * 
 * 
 * You must not remove this notice, or any other, from this software.
 *
 * 
 *************************************************************************/


using System;
using System.Text;
using System.Web;

namespace Circus.Utility {

    /// <summary>
    /// 提供字符编码的辅助函数。
    /// </summary>
    public static partial class EncodeHelper {

        /// <summary>
        /// 对 URL 字符串进行编码。
        /// </summary>
        /// <param name="value">要编码的文本。</param>
        /// <returns>编码的字符串。</returns>
        public static string encodeURI(string value) {
            return HttpUtility.UrlEncode(value);
        }

        /// <summary>
        /// 将已经为在 URL 中传输而编码的字符串转换为解码的字符串。
        /// </summary>
        /// <param name="value">要解码的字符串。</param>
        /// <returns>一个已解码的字符串。</returns>
        public static string decodeURI(string value) {
            return HttpUtility.UrlDecode(value);
        }

        /// <summary>
        /// 对 Unicode 字符串进行编码。
        /// </summary>
        /// <param name="value">要编码的文本。</param>
        /// <returns>编码的字符串。</returns>
        public static string escape(string value) {
            StringBuilder sb = new StringBuilder();
            byte[] byteArr = System.Text.Encoding.Unicode.GetBytes(value);
            for (int i = 0; i < byteArr.Length; i += 2) {
                sb.Append("%u");
                sb.Append(byteArr[i + 1].ToString("X2"));//把字节转换为十六进制的字符串表现形式
                sb.Append(byteArr[i].ToString("X2"));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 将已经编码的字符串转换为已解码的字符串。
        /// </summary>
        /// <param name="value">要解码的字符串。</param>
        /// <returns>一个已解码的字符串。</returns>
        public static string unescape(string value) {
            string str = value.Remove(0, 2);//删除最前面两个＂%u＂
            string[] strArr = str.Split(new string[] { "%u" }, StringSplitOptions.None);//以子字符串＂%u＂分隔
            byte[] byteArr = new byte[strArr.Length * 2];
            for (int i = 0, j = 0; i < strArr.Length; i++, j += 2) {
                byteArr[j + 1] = Convert.ToByte(strArr[i].Substring(0, 2), 16);  //把十六进制形式的字串符串转换为二进制字节
                byteArr[j] = Convert.ToByte(strArr[i].Substring(2, 2), 16);
            }
            str = System.Text.Encoding.Unicode.GetString(byteArr);　//把字节转为unicode编码
            return str;
        }

        /// <summary>
        /// 对字符串进行编码。
        /// </summary>
        /// <param name="value">要编码的字符串。</param>
        /// <param name="addDoubleQuotes">一个值，该值指示是否用双引号括住编码的字符串。</param>
        /// <returns>编码的字符串。</returns>
        public static string encodeJavaScriptString(string value, bool addDoubleQuotes = false) {
            return HttpUtility.JavaScriptStringEncode(value, addDoubleQuotes);
        }

        /// <summary>
        /// 将字符串最小限度地转换为 HTML 编码的字符串。
        /// </summary>
        /// <param name="value">要编码的字符串。</param>
        /// <returns>编码的字符串。</returns>
        public static string encodeHtmlAttribute(string value) {
            return HttpUtility.HtmlAttributeEncode(value);
        }

        /// <summary>
        /// 将对象的字符串表示形式转换为 HTML 编码的字符串，并返回编码的字符串。
        /// </summary>
        /// <param name="value">要编码的字符串。</param>
        /// <returns>编码的字符串。</returns>
        public static string encodeHtml(string value) {
            return HttpUtility.HtmlEncode(value);
        }

        /// <summary>
        /// 将已经为 HTTP 传输进行过 HTML 编码的字符串转换为已解码的字符串。
        /// </summary>
        /// <param name="value">要解码的字符串。</param>
        /// <returns>一个已解码的字符串。</returns>
        public static string decodeHtml(string value) {
            return HttpUtility.HtmlDecode(value);
        }

    }

}
